package hanjia;

import java.util.Arrays;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/9-下午3:03
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode105 {


    public static TreeNode buildTree(int[] preorder, int[] inorder) {
        int preLength = preorder.length;
        int inLength = inorder.length;
        if (preLength==0){
            return null;
        }

        if (preLength==1){
            return new TreeNode(preorder[0],null,null);
        }

        int root = preorder[0];
        int count = 0;
        int index = 0;
        for (int i : inorder) {
            if (i == root) {
                count = index;
                break;
            }
            index++;
        }
        int[] leftOrder = Arrays.copyOfRange(inorder, 0, count);
        int[] rightInorder = Arrays.copyOfRange(inorder, count + 1, inorder.length);
        int[] leftPreorder = Arrays.copyOfRange(preorder, 1, leftOrder.length + 1);
        int[] rightPreorder = Arrays.copyOfRange(preorder, leftOrder.length + 1, preorder.length);
        TreeNode treeNode = new TreeNode(root, buildTree(leftPreorder, leftOrder), buildTree(rightPreorder, rightInorder));
        return treeNode;

    }




}
